<?php
namespace app\backend\controller;
use think\Db;
use \think\Validate;
use think\Paginator;
use think\Controller;
class Product extends Controller
{
	  public function product_add()
    {
        return view('product/product_add');
    }

    public function product_list()
    {
      $ret=db('product')->paginate(5);
      if(count($ret)!=0)
      {     
        foreach ($ret as $val) {
              if($val['product_sort']==0)
              {
                $val['product_sort']='景点';
                $data[]=$val;
       
              }
              else if($val['product_sort']==1)
              {
                $val['product_sort']='酒店';
                $data[]=$val;

              }
              else
              {
                $val['product_sort']='餐厅';
                $data[]=$val;
              }
            }
      }
      else
      {
        $data=[];
      }
      $this->assign('ret',$ret);
      $this->assign('data',$data);
      return view('product/product_list');
    }
 
    public function product_insert()
    {
        
        $product_name=input('product_name');
        $product_number=input('product_number');
        $product_img=input('imgSrc');
        $product_detail=input('product_detail');
        $product_count=input('product_count');
        $product_price=input('product_price');
        $product_sort=input('product_sort');


        $rule = [
                'product_name'  => 'require',
                'product_number'=>'require',
                'product_img' => 'require',              
                'product_detail' => 'require',
                'product_count' => 'require',
                'product_price' => 'require',
                'product_sort' => 'require'
            ];
        $msg = [
            'product_name.require' => '产品名称不能为空！',   
            'product_number.require'  => '产品编号不能为空！',
            'product_img.require'  => '产品图片不能为空！',              
            'product_detail.require'  => '产品详情不能为空！',
            'product_count.require'  => '产品库存不能为空！',
            'product_price.require'  => '产品价格不能为空！',
            'product_sort.require' => '产品种类不能为空！'
            
      ];
        
        $data=[
            'product_name'=>$product_name,
            'product_number'=>$product_number,
            'product_img'=>$product_img,
            'product_detail'=>$product_detail,
            'product_price'=>$product_price,
            'product_count'=>$product_count,
            'product_sort'=>$product_sort
            
        ];

        $validate = new Validate($rule, $msg);
        $result   = $validate->check($data);
        if (!$result ) {
              $this->error($validate->getError());
          }
          $ret=db('product')->insert($data);
           
       if($ret==false)
         {
         
          $this->error("插入失败");
         }
         
          $this->success('插入成功','backend/product/product_list'); 
    }
     public function product_edit()
    {
    	    $product_id=input('get.product_id');
    if($product_id!="")
    {
      $result=db('product')->where("product_id={$product_id}")->select();
     

    }
        $this->assign('result',$result);
  
       
    	return view('product/product_edit');
    }

    public function product_update()
    {
        //$product_sort=input('post.product_visible');
        $product_name=input('post.product_name');
        $product_num=input('post.product_number');
        $product_detail=$_POST['product_detail'];
        $product_sort=$_POST['product_sort'];
        $product_count=input('product_count');
        $product_price=input('product_price');
     
        $product_img=isset($_POST['imgSrc'])?$_POST['imgSrc']:'';
       if($product_img!='')
        {
          $updata=[
                'product_name'=>$product_name,
                'product_number'=>$product_num,
                'product_img'=>$product_img,
                'product_detail'=>$product_detail,
                'product_price'=>$product_price,
                'product_count'=>$product_count,
                'product_sort'=>$product_sort
          ];
        }
        else{
             $updata=[
                'product_name'=>$product_name,
                'product_number'=>$product_num,
                'product_detail'=>$product_detail,
                'product_price'=>$product_price,
                'product_count'=>$product_count,
                'product_sort'=>$product_sort
              ];
           }

       $ret=db('product')->where("product_number={$product_num}")->update($updata);
       if($ret===0)
    {
      $this->error("没有任何修改");
    }
     if($ret===false)
    {
      $this->error("修改失败");
    }
     $this->success('修改成功','backend/product/product_list');
    }

     public function upimg()
    {
    	$file = request()->file('product_img');
    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'product');
	        if($info){
	            // 成功上传后 获取上传信息
	            // 输出 jpg
	            
	            // 输出 42a79759f284b767dfcb2a0197904287.jpg
	            $img_src="/product/{$info->getSaveName()}"; 
	           echo $img_src;
	          

			        }else{
			            // 上传失败获取错误信息
			           $this->error($file->getError());
			        }
	       }
    } 

     public function product_delete()
    {
      $product_id=input('get.product_id');
    if($product_id!="")
    {
      $ret=db('product')->where("product_id={$product_id}")->delete();
      if($ret==false)
         {
            $this->error('删除失败');
          }
             $this->success('删除成功');

    }
    }
 
}